package com.sczmgk.track.activity;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sczmgk.track.R;
import com.sczmgk.track.activity.trackactivity.SignDetailActivity;
import com.sczmgk.track.constant.Constant;
import com.sczmgk.track.entity.SignBean;
import com.sczmgk.track.entity.bean.RecordData;
import com.sczmgk.track.entity.bean.SignListBean;
import com.sczmgk.track.httpUtils.MyHttp;
import com.sczmgk.track.test.MyCallback;
import com.sczmgk.track.utils.CacheUtils;
import com.sczmgk.track.utils.Tools;
import com.sczmgk.track.utils.UIUtils;
import com.sczmgk.track.view.refreshview.RefreshListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SignRecordActivity extends BaseRefreshActivity {

    protected final int REFRESH_LIST = 1;
    protected final int HIDE_FOOTER_HEADER = 102;

    private List<SignBean> list = new ArrayList<>();
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_record);
        setTitle("签到记录");
        intiView();
        setListeners();
        loadData(1, "", LOAD_NORMAL);
    }

    @Override
    public void loadData(int page, String keyword, final int loadStatus) {


        if (loadStatus == LOAD_NORMAL) {
            showLoadingDiaLog();
        }

        MyHttp.requestDataT(this, getRquestUrl(), MyHttp.POST, null, getRequestData(page, keyword), null, false, null, new MyCallback<RecordData>() {
            @Override
            public void onSuccess(RecordData o) {
                dismissLoadingDiaLog();
                //记录当前的页码
                processData(o.data,loadStatus);
            }

            @Override
            public void onUnknownError() {

            }

            @Override
            public void onError(String msg) {
                dismissLoadingDiaLog();
                mHandler.sendEmptyMessage(HIDE_FOOTER_HEADER);
                showErrorMsg(msg);
            }

            @Override
            public void onSuccess() {

            }
        });


    }

    private void processData(SignListBean data,int loadStatus) {
        totalpages = data.totalpages;
        nowpage = data.nowpage;

        if(loadStatus == LOAD_NORMAL){//清空集合
            if(data.datalist != null){
                list.clear();
                list.addAll(data.datalist);
            }

        }else if(loadStatus == LOAD_MORE){

            list.addAll(data.datalist);
        }

        refreshList();
        mHandler.sendEmptyMessage(HIDE_FOOTER_HEADER);
    }

    @Override
    public void setListeners() {
        super.setListeners();

    }

    private void intiView() {
        mListView = (RefreshListView) findViewById(R.id.prlv_sign_record);
        adapter = new MyAdapter();
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SignBean signBean = list.get(position-1);
                UIUtils.startActivity(SignRecordActivity.this, SignDetailActivity.class,"location",signBean);
            }
        });
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case REFRESH_LIST:

                adapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    public boolean getIsNeedLogin() {
        return false;
    }

    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return ((list == null || list.size() == 0) ? 0 : list.size());
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = View.inflate(SignRecordActivity.this, R.layout.item_sign_record, null);
                viewHolder.address = (TextView) convertView.findViewById(R.id.tv_address);
                viewHolder.name = (TextView) convertView.findViewById(R.id.tv_name);
                viewHolder.time = (TextView) convertView.findViewById(R.id.tv_time);
                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            SignBean signBean = list.get(position);
            viewHolder.address.setText(signBean.signaddress);
            viewHolder.name.setText(signBean.companyname);
            viewHolder.time.setText(signBean.signtime);
            return convertView;
        }
    }

    public class ViewHolder {
        TextView time;
        TextView name;
        TextView address;
    }

    @Override
    public Map<String, Object> getRequestData(int page, String keyword) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("userid", CacheUtils.getLoginUserid(SignRecordActivity.this));
        maps.put("pageindex", page);
        maps.put("keyword", Tools.isEmpty(keyword) ? "" : keyword);
        return maps;
    }

    @Override
    public String getRquestUrl() {
        return Constant.RECORD_SIGN;
    }
}
